/* main.c */
#include "app_inc.h"

static void ADC_Configuration(void);

int main(void)
{
    BSP_InitSystem();
    BSP_InitStdioLPUART(115200U);
    BSP_InitAdcBatterySensor();

    ADC_Configuration();

    printf("\r\n\r\nPowerSensor Demo.\r\n");
    printf("Compiled on %s, %s\r\n\r\n", __DATE__, __TIME__);

    printf("Press any key to get battery voltage value.\r\n");

    while (1)
    {
        getchar();

        while(!ADC_HAL_IsChannelConvDone(BSP_ADC_BATTERY_SENSOR_PORT, BSP_ADC_BATTERY_SENSOR_CAHNNEL_GROUP))
        {}
        printf("Battery voltage value: %d\r\n",
            ADC_HAL_GetChannelConvValue(BSP_ADC_BATTERY_SENSOR_PORT, BSP_ADC_BATTERY_SENSOR_CAHNNEL_GROUP) );
    }
}

static void ADC_Configuration(void)
{
    ADC_ConverterConfig_T adcConverterConfigStruct;
    ADC_ConvCommand_T adcConvCommandStruct;

    adcConverterConfigStruct.RefVoltSource          = eADC_RefVoltSourceSel_Vref;
    adcConverterConfigStruct.ClockSource            = eADC_ClockSourceSel_Alt0;
    adcConverterConfigStruct.ClockDivider           = eADC_ClockDividerSel_8;
    adcConverterConfigStruct.ResolutionBits         = eADC_ResolutionBits_12;
    adcConverterConfigStruct.SampleClockCount       = 0x3F;
    ADC_HAL_ConfigConverter(BSP_ADC_BATTERY_SENSOR_PORT, &adcConverterConfigStruct);
    ADC_HAL_SetConvTriggerMode(BSP_ADC_BATTERY_SENSOR_PORT, eADC_ConvTriggerBySoftware);
    ADC_HAL_SetHardwareAverageMode(BSP_ADC_BATTERY_SENSOR_PORT, eADC_HardwareAverageCount32);
    ADC_HAL_DoAutoCalibration(BSP_ADC_BATTERY_SENSOR_PORT);
    ADC_HAL_EnableContinuousConvMode(BSP_ADC_BATTERY_SENSOR_PORT, true); /* 连续转换模式 */

    adcConvCommandStruct.ConvChannelNum = BSP_ADC_BATTERY_SENSOR_CAHNNEL_INDEX;
    adcConvCommandStruct.enInterruptOnCovnDone = false;
    ADC_HAL_SetChannelConvCmd(BSP_ADC_BATTERY_SENSOR_PORT,
        BSP_ADC_BATTERY_SENSOR_CAHNNEL_GROUP, &adcConvCommandStruct);

}

/* EOF. */

